#include "CPlayerObject.h"
#include "../CGame.h"
#include "../core/CDirect3D.h"
#include "../core/CCamera.h"
#include "../core/CRetroInput.h"
#include "CObjectManager.h"
#include "CBaseEnemy.h"

CPlayerObject::CPlayerObject(void)
{
}

CPlayerObject::~CPlayerObject(void)
{
}

void CPlayerObject::Initialize()
{
	CBaseObject::Initialize();
}

void CPlayerObject::Initialize(int posX, int posY, float depth, int collisionWidth, int collisionHeight)
{
	CBaseObject::Initialize(posX, posY, depth, collisionWidth, collisionHeight);
	m_pObjMan = CObjectManager::GetInstance();
	m_pCamera = NULL;
}

void CPlayerObject::Input(CRetroInput* pInput)
{
	memset(&m_ptAccel, 0, sizeof(POINT));
	if(pInput->CheckAction(ACTION_UP))
	{
		m_ptAccel.y += -500;
	}
	if(pInput->CheckAction(ACTION_DOWN))
	{
		m_ptAccel.y += 500;
	}
	if(pInput->CheckAction(ACTION_LEFT))
	{
		m_ptAccel.x += -500;
	}
	if(pInput->CheckAction(ACTION_RIGHT))
	{
		m_ptAccel.x += 500;
	}
	if(pInput->CheckAction(ACTION_FIRE) && m_pCamera != NULL)
	{
		// get the cursor
		vec3<float> cursor = CGame::GetInstance()->GetCursorPosition();
		POINT cursorPos;
		cursorPos.x = static_cast<LONG>(cursor.x);
		cursorPos.y = static_cast<LONG>(cursor.y);

		// get player position into screen space
		POINT playerPosScreen = m_vfPosition;
		POINT camPos = m_pCamera->GetCameraOffset();
		playerPosScreen.x -= camPos.x;
		playerPosScreen.y -= camPos.y;

		// get the direction to the cursor
		vec3<float> direction;
 		direction.x = static_cast<float>(cursorPos.x - playerPosScreen.x);
		direction.y = static_cast<float>(cursorPos.y - playerPosScreen.y);
		direction.z = 1.0f;
		direction.normalize();

		m_pObjMan->CreateProjectile(m_vfPosition.x, m_vfPosition.y, (direction.x * 10.0f), (direction.y * 10.0f));
	}
}

void CPlayerObject::Update(float fDeltaTime)
{
	m_vfPosition.x += static_cast<long>(static_cast<float>(m_ptAccel.x) * fDeltaTime);
	m_vfPosition.y += static_cast<long>(static_cast<float>(m_ptAccel.y) * fDeltaTime);
}

void CPlayerObject::Render(CDirect3D* pRender, CCamera* pCamera)
{
	if(m_pCamera == NULL)
		m_pCamera = pCamera;
	const POINT& camPos = pCamera->GetCameraOffset();
	RECT drawBox;
	drawBox.left	=	m_vfCollisionBox.left	+ m_vfPosition.x - camPos.x;
	drawBox.top		=	m_vfCollisionBox.top	+ m_vfPosition.y - camPos.y;
	drawBox.right	=	m_vfCollisionBox.right	+ m_vfPosition.x - camPos.x;
	drawBox.bottom	=	m_vfCollisionBox.bottom	+ m_vfPosition.y - camPos.y;
	pRender->DrawRect(drawBox, 128, 0, 0);
}

void CPlayerObject::Destroy()
{
}

bool CPlayerObject::CheckCollision(IBaseObject* pObject)
{
	switch(pObject->Type())
	{
	case OBJECT_ENEMY:
		{
			CBaseEnemy* pEnemy = (CBaseEnemy*)pObject;

			// my collision box
			RECT myBox;
			myBox.left		=	m_vfCollisionBox.left	+ m_vfPosition.x;
			myBox.top		=	m_vfCollisionBox.top	+ m_vfPosition.y;
			myBox.right		=	m_vfCollisionBox.right	+ m_vfPosition.x;
			myBox.bottom	=	m_vfCollisionBox.bottom	+ m_vfPosition.y;

			// enemy collision box
			const RECT& enemyBox = pEnemy->GetCollisionBox();
			const POINT& otherPos = pEnemy->GetPosition();
			RECT otherBox;
			memcpy(&otherBox, &enemyBox, sizeof(RECT));
			otherBox.left	+= otherPos.x;
			otherBox.top	+= otherPos.y;
			otherBox.right	+= otherPos.x;
			otherBox.bottom	+= otherPos.y;
			
			// check collision
			RECT overlap;
			BOOL colide = IntersectRect(&overlap, &myBox, &otherBox);
			if(colide)//abs(dx) <= w && abs(dy) <= h)
			{
				POINT dimentions;
				dimentions.x = overlap.right - overlap.left;
				dimentions.y = overlap.bottom - overlap.top;

				// determin the side
				float w = 0.5f * static_cast<float>((myBox.right - myBox.left) + (otherBox.right - otherBox.left));
				float h = 0.5f * static_cast<float>((myBox.bottom - myBox.top) + (otherBox.bottom - otherBox.top));
				float dx = static_cast<float>(m_vfPosition.x - otherPos.x);
				float dy = static_cast<float>(m_vfPosition.y - otherPos.y);
				float wy = w * dy;
				float hx = h * dx;
				
				if (wy > hx)
				{
					if (wy > -hx)
					{
						// On the top
						m_vfPosition.y += dimentions.y;
					}
					else
					{
						// on the left
						m_vfPosition.x -= dimentions.x;
					}
				}
				else
				{
					if (wy > -hx)
					{
						// on the right
						m_vfPosition.x += dimentions.x;
					}
					else
					{
						// at the bottom
						m_vfPosition.y -= dimentions.y;
					}

				}
				return true;
			}

		} break;
	}
	return false;
}